﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.IO;
using DataAccess;

namespace Manager
{
    public partial class Setting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitEditor();
            if (!Page.IsPostBack)
            {
                Load_Info();
            }
        }

        protected void InitEditor()
        {
            editorAbout.UIColor = System.Drawing.Color.DarkGreen;
            editorAbout.config.toolbar = new object[]
			{
				new object[] { "Source" , "-" ,"Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
				new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
				new object[] { "Link", "Unlink", "Anchor" },
				new object[] { "Image", "Smiley" },
				"/",
				new object[] { "Styles", "Format", "Font", "FontSize" },
				new object[] { "TextColor", "BGColor" }
			};
        }

        private void Load_Info()
        {
            SettingInfo setting = SettingData.SelectBySiteID(Config.Global.Settings.SiteID);

            txtName.Text = setting.SiteName;
            if (setting.SiteLogo == "")
            {
                imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
            }
            else
            {
                imgThumb.ImageUrl = Config.Global.Settings.ImagePath + setting.SiteLogo;
            }
            txtURL.Text = setting.SiteURL;
            txtAddress.Text = setting.SiteAddress;
            txtEmail.Text = setting.SiteEmail;
            txtPhone.Text = setting.SitePhone;
            txtYear.Text = setting.SiteYear;
            txtDes.Text = setting.SiteDescription;
            txtKeywords.Text = setting.SiteKeyword;
            if (setting.SiteStatus == true)
            {
                rbtActive.Checked = true;
            } 
            else
            {
                rbtDeactive.Checked = true;
            }
            editorAbout.Text = setting.SiteAbout;
        }

        protected void lblDel_Click(object sender, EventArgs e)
        {
            imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
            hiddenImg.Value = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() == "")
            {
                lblErrName.Visible = true;
                lblErrName.Text = "Bạn chưa nhập Tên Trang Web.";
                return;
            }
            if ((txtEmail.Text.Trim() != "") && (!Library.Validate.IsEmail(txtEmail.Text.Trim())))
            {
                lblErrEmail.Visible = true;
                lblErrEmail.Text = "Email không đúng. Xin vui lòng nhập lại.";
                return;
            }
            SettingInfo newSetting = new SettingInfo();

            newSetting.SiteID = Config.Global.Settings.SiteID;
            newSetting.SiteName = txtName.Text.Trim();
            string img = hiddenImg.Value;
            int pos = img.LastIndexOf('/');
            img = img.Substring(pos + 1);
            if (Session["sesFileName"] != null)
            {
                newSetting.SiteLogo = Session["sesFileName"].ToString();
            }
            else
            {
                newSetting.SiteLogo = img;
            }
            newSetting.SiteURL = txtURL.Text.Trim();
            newSetting.SiteAddress = txtAddress.Text.Trim();
            newSetting.SiteEmail = txtEmail.Text.Trim();
            newSetting.SitePhone = txtPhone.Text.Trim();
            newSetting.SiteYear = txtYear.Text.Trim();
            newSetting.SiteDescription = txtDes.Text.Trim();
            newSetting.SiteKeyword = txtKeywords.Text.Trim();
            newSetting.SiteAbout = editorAbout.Text;
            if (rbtActive.Checked)
            {
                newSetting.SiteStatus = true;
            } 
            else
            {
                newSetting.SiteStatus = false;
            }

            try
            {
                SettingData.Update(newSetting);
                lblMessage.Visible = true;
                lblMessage.Text = "Cập nhật thành công.";
            }
            catch (System.Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Lỗi: " + ex.Message;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}